package com.xy.iot.common.result.message.base;

import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.xy.iot.common.enums.MessageFormatDataEnum;
import com.xy.iot.common.message.BaseMessage;
import com.xy.iot.common.utils.CommonUtil;
import com.xy.iot.common.utils.FastJsonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * @author: niuniu
 * @Date: 2024/6/10 14:31
 * @Description:
 */
@Slf4j
public class BaseMessageData implements BaseMessage {
    @Setter
    @Getter
    @ApiModelProperty("设备ip")
    private String deviceIp;
    @Setter
    @Getter
    @ApiModelProperty("设备组编码")
    private String deviceGroup;
    @Setter
    @Getter
    @ApiModelProperty("指令类型")
    private int command;
    @Setter
    @Getter
    @ApiModelProperty("消息数据类型")
    private int dataType;
    @Setter
    @Getter
    @ApiModelProperty("消息数据类型")
    private Map<String, Object> commandData;
    @ApiModelProperty("返回消息字节长度")
    @JSONField(serialize = false)
    private int length;
    @ApiModelProperty("返回消息的字节数组")
    @JSONField(serialize = false)
    private byte[] data;


    @Override
    public String toString() {
        return new StringJoiner(", ", BaseMessageData.class.getSimpleName() + "[", "]")
                .add("deviceIp='" + deviceIp + "'")
                .add("deviceGroup='" + deviceGroup + "'")
                .add("command=" + command)
                .add("dataType=" + dataType)
                .add("commandData=" + commandData)
                .toString();
    }

    /**
     * 得到消息数据类型字符串
     * @param enumKey 枚举唯一标识
     */
    public String getMessageDataEnumStr(int enumKey){
        MessageFormatDataEnum responseTypeEnum = MessageFormatDataEnum.getMessageFormatDataEnum(enumKey);
        return responseTypeEnum !=null ? responseTypeEnum.getValue(): null;
    }

    @Override
    public int getLength() {
        if (data!=null){
            length = data.length;
        }
        return length;
    }

    @Override
    public byte[] toBytes() {

        return data;
    }

    @Override
    public void buildResponseData() {
            String responseData = FastJsonUtil.toJSONStr(this);
            MessageFormatDataEnum messageDataEnum = MessageFormatDataEnum.getMessageFormatDataEnum(dataType);
            if (Objects.isNull(messageDataEnum)){
                throw new RuntimeException("暂不支持其他格式");
            }
            switch (messageDataEnum){
                case JSON:
                    this.data = responseData.getBytes(StandardCharsets.UTF_8);
                    break;
                case ASCII:
                    this.data = responseData.getBytes(StandardCharsets.US_ASCII);
                    break;
                case UTF_8:
                    this.data = responseData.getBytes(StandardCharsets.UTF_8);
                    break;
                case GBK:
                    this.data = responseData.getBytes(CharsetUtil.CHARSET_GBK);
                    break;
                case HEX:
                    this.data = CommonUtil.encodeHexStr(responseData).getBytes(StandardCharsets.UTF_8);
                default:
                    throw new RuntimeException("暂不支持其他格式");
            }

    }
}
